using System;
using Microsoft.VisualStudio.Shell;

namespace DslCrossModelFramework {
    /// <summary>
    /// Marks a Dsl Package as being exportable in the Dsl Integration Service
    /// </summary>
    public sealed class DslMetaDataAttribute : RegistrationAttribute {
        public Type DomainModelType {
            get { return domainModelType; }
            set { domainModelType = value; }
        }
        private Type domainModelType;
        public Type SerializationHelperType {
            get { return serializationHelperType; }
            set { serializationHelperType = value; }
        }
        private Type serializationHelperType;

        public Type DomainModelRootType {
            get { return domainModelRootType; }
            set { domainModelRootType = value; }
        }

        private Type domainModelRootType;
        

        public Type SerializationBehaviorType {
            get { return serializationBehaviorType; }
            set { serializationBehaviorType = value; }
        }

        private Type serializationBehaviorType;

        public Type DomainModelMonikerResolverType {
            get { return domainModelMonikerResolverType; }
            set { domainModelMonikerResolverType = value; }
        }

        private Type domainModelMonikerResolverType;

        public DslMetaDataAttribute(Type domainModelType, Type serializationHelperType, Type domainModelRootType, Type serializationBehaviorType, Type domainModelMonikerResolverType) {
            this.domainModelType = domainModelType;
            this.serializationHelperType = serializationHelperType;
            this.domainModelRootType = domainModelRootType;
            this.serializationBehaviorType = serializationBehaviorType;
            this.domainModelMonikerResolverType = domainModelMonikerResolverType;
        }

        public override void Register(RegistrationContext context) {
            if (context == null) {
                throw new ArgumentNullException("context");
            }
            string registryGuidFormat = GetRegistryGuidFormat(context.ComponentType.GUID.ToString());
            using (RegistrationAttribute.Key key = context.CreateKey(@"DslTools\Packages\" + registryGuidFormat)) {
                //context.Log.WriteLine(string.Format("CodeBase: {0}", context.CodeBase));
                //context.Log.WriteLine(string.Format("ComponentPath: {0}", context.ComponentPath));
                //context.Log.WriteLine(string.Format("ComponentType: {0}", context.ComponentType));
                //context.Log.WriteLine(string.Format("InprocServerPath: {0}", context.InprocServerPath));
                //context.Log.WriteLine(string.Format("RegistrationMethod: {0}", context.RegistrationMethod));
                //context.Log.WriteLine(string.Format("RootFolder: {0}", context.RootFolder));

                key.SetValue(Resources.Strings.RegistryIntegrationExportable, 1);
                context.Log.WriteLine("DSL Meta Data: Added exportable attribute");
                key.SetValue(Resources.Strings.RegistryIntegrationNamespace, this.domainModelType.Namespace);
                context.Log.WriteLine("DSL Meta Data: Added namespace attribute");
                key.SetValue(Resources.Strings.RegistryIntegrationDomainModelType, this.domainModelType.AssemblyQualifiedName);
                context.Log.WriteLine(string.Format("DSL Meta Data: Added domainModelType attribute as {0}", this.domainModelType.AssemblyQualifiedName));
                key.SetValue(Resources.Strings.RegistryIntegrationDomainModelRootType, this.domainModelRootType.AssemblyQualifiedName);
                context.Log.WriteLine(string.Format("DSL Meta Data: Added domainModelRootType attribute as {0}", this.domainModelRootType.AssemblyQualifiedName));
                key.SetValue(Resources.Strings.RegistryIntegrationSerializationHelperType, this.serializationHelperType.AssemblyQualifiedName);
                context.Log.WriteLine(string.Format("DSL Meta Data: Added serializationHelperType attribute as {0}", this.serializationHelperType.AssemblyQualifiedName));
                key.SetValue(Resources.Strings.RegistryIntegrationSerializationBehaviorType, this.serializationBehaviorType.AssemblyQualifiedName);
                context.Log.WriteLine(string.Format("DSL Meta Data: Added serializationBehaviorType attribute as {0}", this.serializationBehaviorType.AssemblyQualifiedName));
                key.SetValue(Resources.Strings.RegistryIntegrationDomainModelMonikerResolverType, this.domainModelMonikerResolverType.AssemblyQualifiedName);
                context.Log.WriteLine(string.Format("DSL Meta Data: Added domainModelMonikerResolverType attribute as {0}", this.domainModelMonikerResolverType.AssemblyQualifiedName));
            }

        }

        private string GetRegistryGuidFormat(string guid) {
            guid = guid.StartsWith("{") ? guid : ("{" + guid);
            guid = guid.EndsWith("}") ? guid : (guid + "}");
            return guid;
        }

        public override void Unregister(RegistrationContext context) {
        }
    }
}